﻿@model Nop.Plugin.DiscountRules.CustomerRoles.Models.RequirementModel
@{
    Layout = "";
}

    <script>
    $(document).ready(function () {
        $('#savecustomerrolesrequirement@(Model.RequirementId)').click(function () {
            var customerRoleId = $("#@Html.IdFor(model => model.CustomerRoleId)").val();
            var discountId = @Model.DiscountId;
            var requirementId = @Model.RequirementId;

            var postData = {
                DiscountId: discountId,
                RequirementId: requirementId,
                CustomerRoleId: customerRoleId
            };
            addAntiForgeryToken(postData);

            $.ajax({
                cache: false,
                type: "POST",
                url: "@(Url.Action("Configure", "DiscountRulesCustomerRoles"))",
                data: postData,
                success: function (data, textStatus, jqXHR) {
                    $('#pnl-save-requirement-result@(Model.RequirementId)').fadeIn("slow").delay(1000).fadeOut("slow");
                    //notify parent if it's a new requirement
                    @if (Model.RequirementId == 0)
                    {
                        <text>$("#discountRequirementContainer").trigger('nopnewdiscountruleadded', [data.NewRequirementId]);</text>
                    }
                },
                error: function (jqXHR, textStatus, errorThrown) {
                    var $alertInfoEl = $("#savecustomerrolesrequirementAlert-info");
                    let response = jqXHR.responseJSON;
                    if (response instanceof Object && response.hasOwnProperty('Errors')) {
                        var errorMessages = '';
                        $.each(response.Errors, function (i, error) {
                            errorMessages += error;
                            if (response.Errors.length - 1 != i)
                                errorMessages += '</br>';
                        });
                        $alertInfoEl.html(errorMessages);
                    } else {
                        // display default error
                        $alertInfoEl.html('@T("Admin.Promotions.Discounts.Requirements.FailedToSave")');
                    }

                    $("#savecustomerrolesrequirementAlert").click();
                }
            });
        });
    });
    </script>

<div class="form-group">
    <div class="col-md-3 requirement-label-col">
        <nop-label asp-for="CustomerRoleId" />
    </div>
    <div class="col-md-9 requirement-data-col">
        <div class="requirement-data-input">
            <nop-select asp-for="CustomerRoleId" asp-items="Model.AvailableCustomerRoles" />
        </div>
        <div class="requirement-data-buttons">
            <input type="button" id="savecustomerrolesrequirement@(Model.RequirementId)" class="btn btn-primary" value="@T("Admin.Common.Save")"/>
        </div>
    </div>
</div>
<div class="form-group">
    <div class="col-md-offset-3 col-md-9 requirement-messages-col">
        <div id="pnl-save-requirement-result@(Model.RequirementId)" style="display: none;" class="text-green margin-t-5">
            @T("Admin.Promotions.Discounts.Requirements.Saved")
        </div>
    </div>
</div>
<nop-alert asp-alert-id="savecustomerrolesrequirementAlert" />